package com.abetsy.vben;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

public class Fnos {

    /**
     * {"reqid":"7731ca8a00000000000000000001","req":"util.crypto.getRSAPub"}
     */

    private static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtVF2XSL0zHYcDyqAs05+y/U/xkAsyF4vhBgw9ZHMf+zv3Gx3AeN0adXwqRi3+D5dxRX84s2orkwYG3lG7vncPkNrKFKNsSYNR54iNl84xqCtMIyT43SSoXjSsk3QwKR30AqedcY9aaTFpTLrRfeWWmYgPmqfIOpU4P4KHzEAaW1qPsLrIwadxyVhVJ4wTWkRub2unw8uv8iMEUeleEs+eXYSLjsRFAE/6/ikdukDBwcDuvkKzQlYGUj5okmfrmRge2CC4PqKHIYHGfol/wzaKDYt90CmfjYgMIHutm9vg8BfuejUoly8X+GCSnRB+TwZf5N9g7V4Y02vfawCtsSkeQIDAQAB";

    private static final String SI = "296352743571";

    private static final String REQ_ID = "7731ca8a00000000000000000002";

    private static SecretKeySpec getSecretKey(String key) {
        try {
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            // 指定算法名称，不同的系统上生成的key是相同的。
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(key.getBytes());
            //AES 要求密钥长度为 128
            kg.init(128, random);
            //生成一个密钥
            SecretKey secretKey = kg.generateKey();
            // 转换为AES专用密钥
            return new SecretKeySpec(secretKey.getEncoded(), "AES");
        } catch (NoSuchAlgorithmException var4) {
            return null;
        }
    }

    public static byte[] generateRandomBytes(int length) {
        byte[] randomBytes = new byte[length];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(randomBytes);
        return randomBytes;
    }

    public static void main(String[] args) {
        String aesKeyStr = "VhY38GEk3irAdUMxMl0TFoe1AtXw6sGj";
        byte[] aesKey = StrUtil.bytes(aesKeyStr, CharsetUtil.CHARSET_UTF_8);
        byte[] aesIv = generateRandomBytes(16);
        String aesIvBase64 = Base64.encodeBase64String(aesIv);

        String req = "encrypted";
        String iv = aesIvBase64;
        String rsa = getEncryptRSA(aesKeyStr);
        String aes = getAES(aesKey, aesIv);

        Map map = new HashMap();
        map.put("req", req);
        map.put("iv", iv);
        map.put("rsa", rsa);
        map.put("aes", aes);
        System.out.println(JSONUtil.toJsonStr(map));


        System.out.println("7731ca8a00000000000000000001".length());
    }

    private static String getAES(byte[] aesKey, byte[] aesIv) {
        String data = "{\"reqid\":\"" + REQ_ID + "\",\"user\":\"anorak\",\"password\":\"gcl083417152\",\"deviceType\":\"Browser\",\"deviceName\":\"Mac OS-Google Chrome\",\"stay\":false,\"req\":\"user.login\",\"si\":\"" + SI + "\"}";
        System.out.println(data);
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, aesKey, aesIv);
        return Base64.encodeBase64String(aes.encrypt(data));
    }

    public static String getEncryptRSA(String aesKeyStr) {
        RSA rsa = new RSA(null, PUBLIC_KEY);
        return Base64.encodeBase64String(rsa.encrypt(StrUtil.bytes(aesKeyStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey));
    }

}
